<?php
namespace module\tv\controller;

use think\Request;
use core\tv\model\VideoCateModel;
use core\tv\logic\VideoCateLogic;
use core\tv\validate\VideoCateValidate;

class VideoCate extends Base
{

    /**
     * 视频分类列表
     *
     * @param Request $request            
     * @return mixed
     */
    public function index(Request $request)
    {
        $this->siteTitle = '视频分类';
        
        //列表
        $model = VideoCateModel::getInstance()->order('sort DESC');
        $this->_page($model);

        //状态下拉
        $videoCateStatus = VideoCateLogic::getInstance()->getVideoCateStatus();
        $this->assign('videoCateStatus',$videoCateStatus);

        return $this->fetch();
    }

    /**
     * 新增视频分类
     *
     * @param Request $request            
     * @return mixed
     */
    public function add(Request $request)
    {
        if($request->isPost()){
            $data = [
                'cate_name' => $request->param('cate_name'),
                'cate_desc' => $request->param('cate_desc'),
                'status' => $request->param('status'),
                'sort' => $request->param('sort', 0)
            ];

            //验证
            $this->_validate(VideoCateValidate::class, $data, 'add');

            //添加
            $this->_add(VideoCateModel::class, $data);
        }else{
            $this->siteTitle = '新增视频分类';

            //状态选择
            $videoCateStatus = VideoCateLogic::getInstance()->getVideoCateStatus();
            $this->assign('videoCateStatus',$videoCateStatus);

            return $this->fetch();
        }
    }

    /**
     * 编辑视频分类
     *
     * @param Request $request            
     * @return mixed
     */
    public function edit(Request $request)
    {
        if($request->isPost()){
            $data = [
                'cate_name' => $request->param('cate_name'),
                'cate_desc' => $request->param('cate_desc'),
                'status' => $request->param('status'),
                'sort' => $request->param('sort', 0)
            ];

            //验证
            $this->_validate(VideoCateValidate::class, $data, 'edit');

            //添加
            $this->_edit(VideoCateModel::class, $data);
        }else{
            $this->siteTitle = '编辑视频分类';

            //获取记录
            $this->_record(VideoCateModel::class);

            //状态选择
            $videoCateStatus = VideoCateLogic::getInstance()->getVideoCateStatus();
            $this->assign('videoCateStatus',$videoCateStatus);

            return $this->fetch();
        }
    }

    /**
     * 修改字段值
     *
     * @param Request $request            
     * @return mixed
     */
    public function modify(Request $request)
    {
        $fields = [
            'sort',
            'status'
        ];
        $this->_modify(VideoCateModel::class, $fields);
    }

    /**
     * 删除视频分类
     * 
     * @param Request $request
     * @return void
     */
    public function delete(Request $request)
    {
        $this->_delete(VideoCateModel::class, false);
    }

    
}